package com.jf3q.admin.controller;

import com.jf3q.admin.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf3q.common.domain.EduTeacher;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.common.vo.SearchTeacherVo;
import com.jf3q.common.vo.TeacherVo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author:xiaojie
 * @create: 2023-04-25 14:08
 * @Description: 教师controller
 */

@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    EduTeacherService eduTeacherService;

    @PostMapping("/save")
    @Operation(summary = "讲师保存接口")
    @PreAuthorize("hasAuthority('teacher:save')")
    public ResultVo save(@RequestBody TeacherVo teacherVo){

        EduTeacher eduTeacher = new EduTeacher();
        BeanUtils.copyProperties(teacherVo,eduTeacher);
        eduTeacherService.saveOrUpdate(eduTeacher);

        return ResultVo.success("添加讲师成功",null);

    }
    @PostMapping("page/{pageNum}")
    @Operation(summary = "讲师分页查询接口")
    @PreAuthorize("hasAuthority('teacher:list')")
    public ResultVo get(@PathVariable Integer pageNum,
                        @RequestBody(required = false) SearchTeacherVo searchTeacherVo){

        Page<EduTeacher> page=eduTeacherService.getPage(pageNum,searchTeacherVo);
        return ResultVo.success("查询所有讲师",page);
    }

    @Operation(summary = "删除讲师")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('teacher:delete')")
    public ResultVo del(@PathVariable String id){

        Boolean flag=eduTeacherService.del(id);
        if (flag) {
            return ResultVo.success("删除成功",null);
        }
        return ResultVo.error("删除失败");

    }
    @GetMapping("/{id}")
    public ResultVo getById(@PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        return ResultVo.success("", teacher);
    }


}
